package com.ljj.controller;

import com.ljj.dto.OrderDTO;
import com.ljj.enums.ResultEnum;
import com.ljj.exception.SellException;
import com.ljj.service.impl.OrderServiceImpl;
import com.lly835.bestpay.rest.type.Get;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * Created by 39716 on 2018/4/14.
 */
@Controller
@RequestMapping("/order")
@Slf4j
public class SellerOrderController {

    @Autowired
    private OrderServiceImpl orderService;
    /**
     * 分页展示数据
     * @param page  第几页
     * @param size  每页显示大小
     * @param map
     * @return
     */
    @GetMapping("/orderList")
    public ModelAndView orderList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                  @RequestParam(value = "size", defaultValue = "15") Integer size,
                                  Map<String,Object> map){
        PageRequest pageRequest = new PageRequest(page - 1, size);
        Page<OrderDTO> orderDTOPage = orderService.findList(pageRequest);
        map.put("orderDTOPage",orderDTOPage);
        map.put("currentPage",page);
        return new ModelAndView("order/list",map);
    }

    /**
     * 取消订单
     * @param orderId   订单id
     * @param map
     * @return
     */
    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam(value = "orderId") String orderId,
                                Map<String,Object> map){
        OrderDTO orderDTO = null;
        try {
            orderDTO = orderService.findOne(orderId);
            orderService.cancel(orderDTO);
        }catch (SellException e){
            //取消失败时，跳到错误页面
            if (orderDTO == null){
                log.error("查询不到订单");
                map.put("msg",e.getMessage());
                map.put("url","/order/orderList");
                return new ModelAndView("common/error");
            }
        }
        map.put("msg", ResultEnum.CANCEL_SUCCESS.getMessage());
        map.put("url","/order/orderList");
        return new ModelAndView("common/success");
    }
    /**
     * 订单详情
     * @param orderId   订单id
     * @param map
     * @return
     */
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam(value = "orderId") String orderId,
                                Map<String,Object> map){
        OrderDTO orderDTO = null;
        try {
            orderDTO = orderService.findOne(orderId);
        }catch (SellException e){
            //查询订单失败时，跳到错误页面
            if (orderDTO == null){
                log.error("查询不到订单");
                map.put("msg",e.getMessage());
                map.put("url","/order/orderList");
                return new ModelAndView("common/error");
            }
        }
        map.put("orderDTO", orderDTO);
        return new ModelAndView("order/detail");
    }
    /**
     * 完结订单
     * @param orderId   订单id
     * @param map
     * @return
     */
    @GetMapping("/finish")
    public ModelAndView finish(@RequestParam(value = "orderId") String orderId,
                               Map<String,Object> map){
        OrderDTO orderDTO = null;
        try {
            orderDTO = orderService.findOne(orderId);
            orderService.finish(orderDTO);
        }catch (SellException e){
            //完结订单失败时，跳到错误页面
            if (orderDTO == null){
                log.error("查询不到订单");
                map.put("msg",e.getMessage());
                map.put("url","/order/orderList");
                return new ModelAndView("common/error");
            }
            if(orderDTO != null){
                log.error("尚未支付");
                map.put("msg",e.getMessage());
                map.put("url","/order/orderList");
                return new ModelAndView("common/error");
            }
        }
        map.put("msg", ResultEnum.ORDER_FINISH_SUCCESS.getMessage());
        map.put("url","/order/orderList");
        return new ModelAndView("common/success");
    }
}
